import java.awt.Color;
import java.io.File;


import javax.sound.midi.*;

public class Consumer implements Runnable {

	
	
	Thread thread;
	Sequence sequence;
	Synthesizer synth;
	MidiChannel chan[];
	
	public Consumer() {

		try
		{
			
			
			MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
			if (devices.length == 0) {
			    System.out.println("No MIDI devices found");
			} else {
			    for (MidiDevice.Info dev : devices) {
			        System.out.println(dev);
			    }
			}
			
		
			
	
			// Creating a blank sequence for recording the live composed music.
			sequence = new Sequence(Sequence.PPQ, 32);
			// Adding a blank track to the sequence
			sequence.createTrack();
			
		

					
			
			//FileInputStream is = new FileInputStream(new File("/usr/lib/jvm/java-6-sun-1.6.0.22/jre/lib/audio/Cambridge_Concert_Flute.sfArk"));
			//File is = new File("/usr/lib/jvm/java-6-sun-1.6.0.22/jre/lib/audio/Voicesolo_Phil's.gm");
			File is = new File("/usr/lib/jvm/java-6-sun-1.6.0.22/jre/lib/audio/c");
			Soundbank sb = MidiSystem.getSoundbank(is);
			//System.out.println(sb.getName()+ sb.getDescription()+sb.getInstruments()[0]);
			synth = MidiSystem.getSynthesizer();
			chan = synth.getChannels();
			synth.open();
			System.out.println(synth.isSoundbankSupported(sb)+" "+synth.loadAllInstruments(sb));
			
			//int i = 20;
			Instrument instruments[] = synth.getLoadedInstruments();
			for (int ii=0; ii<instruments.length; ii++) {
			      System.out.println(instruments[ii]);
			    }

			
			//String name = instruments[ i ].getName();
			
			//System.out.println( "Soundbank instrument " + i + ": " + name );
			
			chan[ 0 ].programChange( 0,0 );
		
			System.out.println("synth="  +synth.getDeviceInfo().toString());
			System.out.println(sb.getName()+ sb.getDescription()+sb.getInstruments()[0]);
			System.out.println("max synthesizer latency ="  +synth.getLatency()+  " us\n");
			
			
		

			
			thread = new Thread(this, "CONSUMER_THREAD");
			System.out.println("New thread: " + thread);
			
			

		}
		catch (Exception e) {
			e.printStackTrace();
			System.exit(1);

		}


	}



	public void startThread() {
		thread.start();// Start the thread
		System.out.println("started thread: " + thread);

	}
	
	
	public synchronized void run()
	{
		MusicalEvent me_temp;
		try {
			synth.open();
			chan[0].programChange(0,1);
			
			
		} catch (MidiUnavailableException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while(true)
			{
			me_temp = Global.musicBuffer.popEvent();
			System.out.println(me_temp.channel);
			System.out.println(me_temp.note);
			System.out.println(me_temp.velocity);
			System.out.println(me_temp.type);
			
		if(me_temp.type==-1) 
		{
			System.out.println("break1 cuz end reached");
			break;
		}
			if (me_temp.type==1) {
				if (chan[0]!=null)
				{
					System.out.println("in run of consumer");
					//chan[0].controlChange(65,127);
					//chan[0].controlChange(5,500);
					//System.out.println(">>"+chan[0].getController(65)+"<<<<");
				chan[0].noteOn(me_temp.note, me_temp.velocity);
				
				try {
					Thread.yield();
					Thread.sleep(me_temp.duration);
					chan[0].noteOff(me_temp.note);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
				else System.out.println("null channel");
			}
			}
		System.out.println("out of consumer run");
		synth.close();
			}
		
	
	}
	
